<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>提取url地址的参数</title>
</head>
<!-- >//https://www.baidu.com/s?name=dingding&age=38&sex=male&
>// 根据上面的url 的参数（name=dingding&age=38&sex=male）
>// 把参数变成一个对象{name:'dingding',age:18,sex:male}  -->

<body>
    <script>
        var url = 'https://www.baidu.com/s?name=dingding&age=38&sex=male&';
        // 截取字符串：name=dingding&age=38&sex=male
        var idx = url.indexOf('?');
        var res = url.substring(idx + 1, url.length - 1);
        //name=dingding&age=38&sex=male
        var arr = res.split('&');
        // arr = ['name=dingding','age=38','sex=male']
        var obj = {};
        // 通过循环把数组的每一个元素都除去
        for (var i = 0; i < arr.length; i++) {
            // arr[i]  = 'name=dingding'
            var newArr = arr[i].split('=');
            // newArr  ['name','dingding']
            // 对象的格式{name:'dingding'}
            obj[newArr[0]] = newArr[1]
        }
        console.log(obj);
    </script>
    
</body>
</html>